using CfgTable;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;

namespace IQIGame.Onigao.GamePlay
{
    public class UIN_EquipInfoDetail : UIBaseNode
    {

        #region Template Generate,don't modify
        protected class UIB_UIN_EquipInfoDetail
        {
            #region ObjectBinding Generate
            public IQIGame.Onigao.Framework.ExButton buttonRight { protected set; get; }
            public IQIGame.Onigao.Framework.ExButton buttonLeft { protected set; get; }
            public UnityEngine.GameObject commonEquip { protected set; get; }
            public IQIGame.Onigao.Framework.ExText nameEquip { protected set; get; }
            public UnityEngine.GameObject toggleTag { protected set; get; }
            public UnityEngine.GameObject panelAttribute { protected set; get; }
            public IQIGame.Onigao.Framework.ExText textLv { protected set; get; }
            public IQIGame.Onigao.Framework.ExProgressBar expBar { protected set; get; }
            public IQIGame.Onigao.Framework.ExArtSpriteNum power { protected set; get; }
            public UnityEngine.GameObject attMould { protected set; get; }
            public UnityEngine.GameObject equipSkinItem { protected set; get; }
            public UnityEngine.UI.ExLoopVerticalScrollRect loopVerticalScrollRect { protected set; get; }
            public UnityEngine.GameObject panelStory { protected set; get; }
            public IQIGame.Onigao.Framework.ExText textDes { protected set; get; }
            public UnityEngine.GameObject desMould { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("buttonRight", out var __tbv0);
                this.buttonRight = __tbv0;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("buttonLeft", out var __tbv1);
                this.buttonLeft = __tbv1;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("commonEquip", out var __tbv2);
                this.commonEquip = __tbv2;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("nameEquip", out var __tbv3);
                this.nameEquip = __tbv3;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("toggleTag", out var __tbv4);
                this.toggleTag = __tbv4;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("panelAttribute", out var __tbv5);
                this.panelAttribute = __tbv5;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("textLv", out var __tbv6);
                this.textLv = __tbv6;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExProgressBar>("expBar", out var __tbv7);
                this.expBar = __tbv7;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExArtSpriteNum>("power", out var __tbv8);
                this.power = __tbv8;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("attMould", out var __tbv9);
                this.attMould = __tbv9;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("equipSkinItem", out var __tbv10);
                this.equipSkinItem = __tbv10;
                __binding.TryGetVariableValue<UnityEngine.UI.ExLoopVerticalScrollRect>("loopVerticalScrollRect", out var __tbv11);
                this.loopVerticalScrollRect = __tbv11;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("panelStory", out var __tbv12);
                this.panelStory = __tbv12;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("textDes", out var __tbv13);
                this.textDes = __tbv13;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("desMould", out var __tbv14);
                this.desMould = __tbv14;
            }
            #endregion ObjectBinding Generate 
        }
        #endregion Template Generate,don't modify

        #region fields & properties
        private UIN_CommonEquip equip = null;
        private IrregularListAdapter<UIN_CommonToggle> tags = null;
        private ListScrollAdapter<UIN_CommonAttribute> attrs = null;
        private ListScrollAdapter<UIN_EquipSkinItem> skins = null;
        private LoopScrollAdapter<UIN_CommonIconDesc> skillDescs = null;
        private ListScrollAdapter<UIN_CommonIconDesc> storys = null;

        private int selectedTag = 0;
        private UD_EquipInfo udEquip = null;
        private int equipCount = 0;
        private int equipIndex = 0;

        protected UIB_UIN_EquipInfoDetail ui { get; set; }
        #endregion

        protected override void BeforeInit()
        {
            ui = new UIB_UIN_EquipInfoDetail();
            ui.InitBinding(this.csObjBind);
        }

        protected override void OnInit()
        {
            this.ui.buttonRight.onClick.AddListener(this.OnClickRight);
            this.ui.buttonLeft.onClick.AddListener(this.OnClickLeft);

            this.equip = this.InitChildNode<UIN_CommonEquip>(this.ui.commonEquip);
            this.equip.SetCallClickView(this.OnClickEquipView);
            this.equip.SetViewActive(true);

            this.tags = this.InitIrregularList<UIN_CommonToggle>(this.ui.toggleTag, this.OnTagChanged);
            this.tags.InitUnitSelected(UnitSelectType.ByExToggle, this.OnTagSelected);

            #region 属性
            this.attrs = this.InitListScroll<UIN_CommonAttribute>(this.ui.attMould, this.OnAttrChanged);
            this.skins = this.InitListScroll<UIN_EquipSkinItem>(this.ui.equipSkinItem, this.OnSkinChanged);
            this.skillDescs = this.InitLoopScroll<UIN_CommonIconDesc>(this.ui.loopVerticalScrollRect, this.OnSkillChanged);
            #endregion

            #region 故事
            this.storys = this.InitListScroll<UIN_CommonIconDesc>(this.ui.desMould, this.OnStoryChanged);
            #endregion
        }

        protected override void OnShow()
        {

        }

        protected override void OnHide()
        {

        }

        private void OnClickRight()
        {
            var ui = this.GetBelongUI();
            ui?.ChangeEquip(true);
        }

        private UIEquipInfo GetBelongUI()
        {
            return this.belongWindow as UIEquipInfo;
        }

        private void OnClickLeft()
        {
            var ui = this.GetBelongUI();
            ui?.ChangeEquip(false);
        }

        private void OnClickEquipView(int index, UIN_CommonEquip item)
        {
            var ui = this.GetBelongUI();
            ui?.ShowEquipPicture();
        }

        protected override void OnDispose()
        {
            this.equip = null;

            this.attrs = null;
            this.skins = null;
            this.skillDescs = null;
            this.storys = null;

            this.selectedTag = 0;
            this.udEquip = null;
            this.equipIndex = 0;
            this.equipCount = 0;
        }

        public void Show(UD_EquipInfo udEquip)
        {
            this.Show();
            this.udEquip = udEquip;
            this.equipCount = udEquip.equipCount;
            this.equipIndex = udEquip.equipIndex;

            this.Refresh(udEquip.equipItem);
            this.tags.RefillCells(2, this.selectedTag);
        }

        private void Refresh(ItemEquipmentData equipItem)
        {
            this.udEquip.ChangeEquip(equipItem);
            this.ViewBtn();
            this.equip.Show(equipItem);
            this.ui.nameEquip.text = equipItem.cfgData.Name;
        }

        private void ViewBtn()
        {
            this.ui.buttonRight.SetActive(this.equipIndex < this.equipCount - 1);
            this.ui.buttonLeft.SetActive(this.equipIndex > 0);
        }

        private void OnTagChanged(UIN_CommonToggle item, int index)
        {
            item.Show(index);
        }

        private void OnTagSelected(int index, UIN_CommonToggle item)
        {
            this.selectedTag = index;
            this.RefreshPanel();
        }

        private void RefreshPanel()
        {
            this.ui.panelAttribute.SetActive(false);
            this.ui.panelStory.SetActive(false);
            switch (this.selectedTag)
            {
                case 1:
                    this.ViewPanelStory();
                    break;
                default:
                    this.ViewPanelAttribute();
                    break;
            }
        }

        #region 属性
        private void ViewPanelAttribute()
        {
            this.ui.panelAttribute.SetActive(true);
            var equipData = this.udEquip.equipItem;
            bool isMaxLv = equipData.isMaxLv;
            int currExp = isMaxLv ? 0 : equipData.exp;
            int nextExp = isMaxLv ? 1 : equipData.cfgLv.NextExp;

            this.ui.textLv.text = equipData.level.ToString();
            this.ui.expBar.SetValue(currExp, nextExp);
            if (isMaxLv)
                this.ui.expBar.text = APIUIConst.GetMaxLabel();

            // TODO power 战力计算
            this.ui.power.SetInt(0);
            this.ShowAttributes();

            int cntSkins = equipData.skinCount;
            int skinSelected = -1;
            this.skins.RefillCells(cntSkins, skinSelected);

            var cfgEquipStar = equipData.cfgStar;
            int cntSkil = cfgEquipStar.SkillIds.Count;
            this.skillDescs.RefillCells(cntSkil);
        }

        private void ShowAttributes()
        {
            this.attrs.RefillCells(this.udEquip.allAttributeCids.Count);
        }

        private void OnAttrChanged(UIN_CommonAttribute item, int index)
        {
            int attrCid = this.udEquip.allAttributeCids[index];
            int attrValue = this.udEquip.GetAttributeValue(attrCid);
            item.Show(attrCid, attrValue);
        }

        private void OnSkinChanged(UIN_EquipSkinItem item, int index)
        {
            var equipData = this.udEquip.equipItem;
            var cfg = equipData.GetCfgEquipSkin(index);
            bool isWear = equipData.skinCid == cfg.Id;
            bool isHas = EquipmentModule.Instance.IsHasEquipSkin(cfg.Id);
            item.Show(cfg, index);
            item.SetClickCall(this.OnSkinSelected);
            item.SetSelected(isWear);
            item.SetUnLocked(isHas);
        }

        private void OnSkinSelected(int index, UIN_EquipSkinItem item)
        {
            var equipData = this.udEquip.equipItem;
            var cfg = equipData.GetCfgEquipSkin(index);
            UD_EquipSkin udSkin = UD_EquipSkin.NewFromPool().Sync(this.udEquip.equipItem, cfg.Id, index);
            this.OpenUI<UIEquipSkin>(udSkin);
        }

        private void OnSkillChanged(UIN_CommonIconDesc item, int index)
        {
            var equipData = this.udEquip.equipItem;
            var cfgEquipStar = equipData.cfgStar;
            CfgSkillLevel cfgLv = TableCenter.skillLevel.GetCfg(cfgEquipStar.SkillIds[index], 1);
            int nState = 1;
            item.Show(nState, cfgLv.EffectText);
        }
        #endregion

        #region 故事
        private void ViewPanelStory()
        {
            this.ui.panelStory.SetActive(true);
            var equipData = this.udEquip.equipItem;
            var cfgStar = equipData.cfgStar;
            var cfgItem = equipData.cfgData;
            this.ui.textDes.text = cfgItem.Describe;

            int cntStar = equipData.maxStar;
            this.storys.RefillCells(cntStar);
        }

        private void OnStoryChanged(UIN_CommonIconDesc item, int index)
        {
            int star = index + 1;
            var equipItem = this.udEquip.equipItem;
            int currStar = equipItem.star;
            int nState = 0;
            string lab = "";
            if (currStar >= star)
            {
                nState = 1;
                var cfg = TableCenter.equipStar.GetCfg(equipItem.cid, star);
                lab = cfg.Story;
            }
            else
            {
                lab = APIUIEquip.GetUnlockDescNeedStar(star);
            }
            item.Show(nState, lab);
        }
        #endregion
    }
}
